<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
    <script type="text/javascript" src="/js/libs/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
    <script src="/js/bootstrap/bootstrap.min.js"></script>
    <script src="/js/bootstrap/bootstrap-select.min.js"></script>
    <script type="text/javascript" src="/js/jq.js"></script>
    <script type="text/javascript" src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <script type="text/javascript" src="/js/common.js"></script>
    <script type="text/javascript" src="/layui/layui.js"></script>
    <script type="text/javascript" src="/js/dict.js"></script>
    <script type="text/javascript" src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
    <script type="text/javascript" src="/js/my/ztree-menu.js"></script>
    <script type="text/javascript" src="/js/my/permission.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
            <input type="hidden" id="id" name="id">
            <input type="hidden" id="flowState" name="flowState">
            <div class='form-group'>
                <label class='col-md-4 control-label'>借用方人员</label>
                <div class='col-md-8'>
                    <input class='form-control' type='hidden' name='borrowerPersonnel' id='borrowerPersonnel'>
                    <input style="width: 90%;float: left;" class='form-control' placeholder='借用方' type='text'
                           name='borrowerPersonnelName' id='borrowerPersonnelName'
                           readonly>
                    <button style="width: 10%;float: right; height: 32px; line-height: 32px;"
                            class="layui-btn layui-btn-sm"
                            onclick="showTableAndFlushFields('user', 'user', '用户', false, 'borrowerPersonnel:id', 'borrowerPersonnelName:nickname', 'borrowerDepartment:deptId', 'borrowerDepartmentName:deptName')">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>

            <div class='form-group'>
                <label class='col-md-4 control-label'>借用方部门</label>
                <div class='col-md-8'>
                    <input class='form-control' type='hidden' name='borrowerDepartment' id='borrowerDepartment'>
                    <input style="width: 90%;float: left;" class='form-control' placeholder='借用方部门' type='text'
                           name='borrowerDepartmentName' id='borrowerDepartmentName' readonly>
                    <button style="width: 10%;float: right; height: 32px; line-height: 32px;"
                            class="layui-btn layui-btn-sm"
                            onclick="showSpecialTree('borrowerDepartment', 'borrowerDepartmentName', 'dept', '部门', 'root', 'select','fasDeptTree', true, true)">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>出借方人员</label>
                <div class='col-md-8'>
                    <input class='form-control' type='hidden' name='lenderPersonnel' id='lenderPersonnel'>
                    <input style="width: 90%;float: left;" class='form-control' placeholder='出借方' type='text'
                           name='lenderPersonnelName' id='lenderPersonnelName'
                           readonly>
                    <button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
                            onclick="showTableAndFlushFields('user', 'user', '用户', true, 'lenderPersonnel:id', 'lenderPersonnelName:nickname', 'lenderDepartment:deptId', 'lenderDepartmentName:deptName')">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>

            <div class='form-group'>
                <label class='col-md-4 control-label'>出借方部门</label>
                <div class='col-md-8'>
                    <input class='form-control' type='hidden' name='lenderDepartment' id='lenderDepartment'>
                    <input style="width: 90%;float: left;" class='form-control' placeholder='出借方部门' type='text'
                           name='lenderDepartmentName' id='lenderDepartmentName' readonly>
                    <button style="width: 10%;float: right; height: 32px; line-height: 32px;"
                            class="layui-btn layui-btn-sm"
                            onclick="showSpecialTree('lenderDepartment', 'lenderDepartmentName', 'dept', '部门', 'root', 'select','fasDeptTree', true, true)">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>商品</label>
                <div class='col-md-8'>
                    <input class='form-control' type='hidden' name='goodsId' id='goodsId' >
                    <input style="width: 90%;float: left;" class='form-control' placeholder='商品' type='text' name='goodsName' id='goodsName' readonly>
                    <button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
                            onclick="showTableAndFlushFields('fas/goods','goods', '商品', true, false, 'goodsId:id', 'goodsName:name', 'specification:specification', 'goodsUnit:unit')">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>商品规格</label>
                <div class='col-md-8'>
                    <input class='form-control' placeholder='商品规格' readonly type='text' name='specification' id='specification'>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>商品单位</label>
                <div class='col-md-8'>
                    <input class='form-control' placeholder='商品单位' readonly type='text' name='goodsUnit' id='goodsUnit'>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>借用数量</label>
                <div class='col-md-8'>
                    <input class='form-control' placeholder='借用数量' type='text' name='borrowerNum' id='borrowerNum'>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>预计归还时间</label>
                <div class='col-md-8'>
                    <input type="text" class="form-control" placeholder="预计归还时间" name = "estimateReturnTime" id="estimateReturnTime">
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>备注</label>
                <div class='col-md-8'>
                    <textarea class="form-control" placeholder="备注" name="remark" id="remark"></textarea>
                </div>
            </div>
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" type="submit" onclick="save()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<script type="text/javascript">
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var layDate = layui.laydate;
        layDate.render({
            elem: '#estimateReturnTime'
            ,done: function(value, date, endDate){
                if ($("#form").data('bootstrapValidator')) {
                    $("#form").data('bootstrapValidator').resetForm();
                }
            }
        });
    });

    // 设置验证规则
    var validatorSettings = {
        message: '验证不通过',
        fields: {
            estimateReturnTime: {
                message: '校验失败',
                validators: {
                    notEmpty: {
                        message: '预计归还时间不能为空'
                    }
                }
            },borrowerNum: {
                message: '校验失败',
                validators: {
                    notEmpty: {
                        message: '借用数量不能为空'
                    }
                }
            },borrowerDepartmentName: {
                message: '校验失败',
                validators: {
                    notEmpty: {
                        message: '借用方部门不能为空'
                    }
                }
            },lenderDepartmentName: {
                message: '校验失败',
                validators: {
                    notEmpty: {
                        message: '出借方部门不能为空'
                    }
                }
            }
        },
        submitHandler: function (validator, form, submitButton) {
            alert("submit");
        }
    };

    $('#form').bootstrapValidator(validatorSettings);

    initData();

    function initData() {
        var id = getUrlParam("id");
        if (id) {
            $.ajax({
                type: 'get',
                url: '/borrow/' + id,
                async: false,
                success: function (json) {
                    if (json.code === 0) {
                        var data = json.data;
                        $('#id').val(data.id);
                        $('#name').val(data.name);
                        $('#lenderPersonnel').val(data.lenderPersonnel);
                        $('#lenderPersonnelName').val(data.lenderPersonnelName);
                        $('#lenderDepartment').val(data.lenderDepartment);
                        $('#lenderDepartmentName').val(data.lenderDepartmentName);
                        $('#borrowerPersonnel').val(data.borrowerPersonnel);
                        $('#borrowerPersonnelName').val(data.borrowerPersonnelName);
                        $('#borrowerDepartment').val(data.borrowerDepartment);
                        $('#borrowerDepartmentName').val(data.borrowerDepartmentName);
                        $('#estimateReturnTime').val(data.estimateReturnTime);
                        $('#goodsId').val(data.goodsId);
                        $('#goodsName').val(data.goodsName);
                        $('#specification').val(data.specification);
                        $('#goodsUnit').val(data.goodsUnit);
                        $('#borrowerNum').val(data.borrowerNum);
                        $('#remark').val(data.remark);
                        $('#flowState').val(data.flowState);
                    }
                }
            });
        }
    }

    function save() {
        $('#form').bootstrapValidator(validatorSettings);
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }

        var formdata = $("#form").serializeObject();
        var flowState = formdata['flowState'];
        if (flowState == '0') {
            $.ajax({
                type: 'put',
                url: '/borrow',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(formdata),
                success: function (json) {
                    if (json.code === 0) {
                        layer.msg("修改成功", {shift: -1, time: 1000}, function () {
                            parent.location.reload();//关闭iFrame
                        });
                    } else {
                        layer.msg("修改失败：" + json.message, {shift: -1, time: 1000});
                    }
                }
            });
        } else {
            layer.msg("已提交后借用单不可修改", {shift: -1, time: 1000});
        }


    }

</script>
</body>
</html>
